package yxw.com.imooccamera;

import android.app.Activity;

import android.content.Intent;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CustomCamera extends Activity implements SurfaceHolder.Callback {
    private Camera mCamera;

    private SurfaceView mPreview;

    private SurfaceHolder mHolder;

    private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        /**
         *
         * @param data  保存着拍照之后所存储的整个相片的数据
         * @param camera
         */
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File tempFile = new File("/sdcard/temp.png");

            try {
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.write(data);
                fos.close();

               Intent intent= new Intent(CustomCamera.this,ResultAty.class);
               intent.putExtra("picPath",tempFile.getAbsolutePath());
               startActivity(intent);
               //将拍照的activit finish掉
               CustomCamera.this.finish();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom);
        mPreview = (SurfaceView) findViewById(R.id.preview);
        mHolder = mPreview.getHolder();
        //把这个acitvity作为回调设置给mHolder
        mHolder.addCallback(this);

        //点击屏幕自动对焦
        mPreview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //只是对焦，不做任何操作，所以参数设置为null
                mCamera.autoFocus(null);
            }
        });

    }


    public void capture(View view) {
//在拍照之前，需要对整个camera的参数进行一个调整，就跟我们使用单反拍照的时候需要进行一些参数设置
        Camera.Parameters params = mCamera.getParameters();
        params.setPictureFormat(ImageFormat.JPEG);
        params.setPreviewSize(800, 600);
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);//自动对焦
        mCamera.autoFocus(new Camera.AutoFocusCallback() {
            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                if (success) {
                    mCamera.takePicture(null, null, mPictureCallback);
                }
            }
        });

    }

    /**
     * 获取Camera对象
     *
     * @return
     */
    private Camera getCamera() {
        /*
        android5.0之后，系统推荐使用Camera2进行更为高级的Camera操作，但是以前的Camera并不是不能用了，大多数时候使用Camera操作就足够了
         */
        Camera camera;
        try {

            camera = Camera.open();
        } catch (Exception e) {
            camera = null;
            e.printStackTrace();
        }

        return camera;
    }

    /**
     * 开始预览相机内容
     */
    private void setStartPreview(Camera camera, SurfaceHolder holder) {
//将camera与surfaceholder进行绑定
        try {
            camera.setPreviewDisplay(holder);
            /*
            这里需要注意一下，系统默认的camera是横评的，所以他予蓝的图像也是横评的
            这里我们给他增加一个方法，让图像竖过来(90度)
             */
            camera.setDisplayOrientation(90);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 释放相机所占用资源
     */
    private void releaseCamera() {
        //mCamera.release();
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            //取消相机的虚拟功能
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }

    /*
    将activity生命周期与上面几个方法进行绑定，保证camera设备是独享的，
    在activity创建的时候去打开camera，在activity销毁的时候去release
    Camera
     */

    @Override
    protected void onResume() {
        super.onResume();
        if (mCamera == null) {
            mCamera = getCamera();
            if (mHolder != null) {
                setStartPreview(mCamera, mHolder);
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseCamera();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        setStartPreview(mCamera, holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //将相机关闭
        mCamera.stopPreview();
//重启相机的功能
        setStartPreview(mCamera, holder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();
    }
}
